#ifndef CINY_CNet
#define CINY_CNet

#include "CInterface.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>


typedef struct Server {
    IServer vtable;  // 虚函数表
    char Name[50];   // 服务器名称
    char IPVersion[10]; // IP版本
    char IP[16];     // IP地址
    int Port;        // 端口号
    int listen_fd;   // listen file descriptor
    IRouter* router;
} Server;

void Server_Start(void* self);
void Server_Stop(void* self);
void Server_Serve(void* self);
void Server_AddRouter(void* self , IRouter* router);


IServer* NewServer(const char* name);



typedef struct Connection{
    IConnect vtable;
    int TCPConnect_fd;
    int connectId;  
    int isClosed;// 这里有个小问题，由于C没有golang中的管道这种好东西，所以，为了达到这个效果，能利用堆和指针，配合轮询达到这种效果
    IRouter* router;
}Connection;

void Connection_Start(void* self);
void Connection_Stop(void* self);
int Connection_GetConnectID(void* self);

void WaitConnectClose(void *self);

IConnect* NewConnection(int* pTCPConnect_fd,int connectId,IRouter* router);



typedef struct Request{
    IRequest vtable;
    IConnect* connection;
    Data* data;
}Request;

IRequest* NewRequest(IConnect* connection,Data* data);

IConnect* RequestGetConnection(IRequest* self);
Data* RequestGetData(IRequest* self);
void RequestHandle(IRequest* self);


typedef struct BaseRouter{
    IRouter vtable;
}BaseRouter;

IRouter* NewRouter();
void BaseRouterBeforeHandle(IRequest* self);
void BaseRouterHandle(IRequest* self);
void BaseRouterAfterHandle(IRequest* self);




Result HandleClientCallBack(void * selfConnection,char* content,int size);
Result HandleRequestCallBack(void* selfRequest);
#endif